package com.retallife.admin.controller;


import com.retallife.admin.exception.Result;
import com.retallife.admin.pojo.Business.BusinessVo;
import com.retallife.admin.pojo.Login.LoginVo;
import com.retallife.admin.pojo.User.UserAddVo;
import com.retallife.admin.pojo.User.UserHeadImageVo;
import com.retallife.admin.pojo.User.UserNameVo;
import com.retallife.admin.pojo.User.UserVo;
import com.retallife.admin.pojo.admin.adminVo;
import com.retallife.admin.service.Admin.AdminService;
import com.retallife.admin.service.Login.LoginService;
import com.retallife.admin.service.User.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "管理员信息类")
@RequestMapping(method = {RequestMethod.POST,RequestMethod.GET})
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    LoginService loginService;

    @Autowired
    UserService userService;

    @ApiOperation(value = "查询Admin表中的所有信息")
    @RequestMapping("adminFindAllAdmin")
    public List<adminVo> adminFindAllAdmin() {
        return adminService.adminFindAllAdmin();
    }

    @ApiOperation(value = "查询business表中的所有信息")
    @RequestMapping("adminFindAllBusiness")
    public List<BusinessVo> adminFindAllBusiness() {
        return adminService.adminFindAllBusiness();
    }

    @ApiOperation(value = "查询User表中的所有信息")
    @RequestMapping("adminFindAllUser")
    public List<UserVo> adminFindAllUser() {
        return adminService.adminFindAllUser();
    }

    @ApiOperation(value = "查询管理员的账号密码是否存在")
    @RequestMapping("findAdminLoginUser")
    public Result findAdminLoginUser(@RequestBody LoginVo LoginVo) {
            int result = loginService.findAdminLoginUser(LoginVo);
            if (result > 0) {
                return Result.ok("登陆成功");
            }
        return Result.ok("登陆失败");
    }

    @ApiOperation(value = "增加User表中的用户信息")
    @RequestMapping("adminAddUser")
    public Result adminAddUser(@RequestBody UserAddVo userAddVo) {
        //判断用户是否重名
        UserNameVo userNameVo  = new UserNameVo();
        String userName1 = userAddVo.getUserName();
        userNameVo.setUserName(userName1);
        List<UserNameVo> userNameVos = adminService.adminFoundUser(userNameVo);

        if (userNameVos.size()==0) {
            int result2 = adminService.adminAddUser(userAddVo);
            if (result2 > 0) {
                //为创建的用户添加默认图片和默认的特征码(进行修改，将空的userHeadImage改成有自定义网址)
                UserHeadImageVo userHeadImageVo = new UserHeadImageVo();

                String headImage = "https://img-blog.csdnimg.cn/20210303194624899.png";
                userHeadImageVo.setUserHeadImage(headImage);
                String state = "0";
                userHeadImageVo.setUserState(state);
                String userName2 = userAddVo.getUserName();
                userHeadImageVo.setUserName(userName2);
                userService.addHeadImageUser(userHeadImageVo);

                return Result.ok("增加成功");
            }
        }
        return Result.ok("增加失败,用户名重复");
    }

    @ApiOperation(value = "删除User表中的用户信息")
    @RequestMapping("adminDeleteUser")
    public Result adminDeleteUser(@RequestBody UserNameVo userNameVo) {
        int result= adminService.adminDeleteUser(userNameVo);
        if (result > 0) {
            return Result.ok("删除成功");
        }
        return Result.ok("删除失败");
    }

    @ApiOperation(value = "修改用户的基本信息(名字没修改)")
    @RequestMapping("adminUpdateUser")
    public Result adminUpdateUser(@RequestBody UserVo userVo) {
        int result = adminService.adminUpdateUser(userVo);
        if (result > 0){
            return Result.ok("修改成功");
        }
        return Result.ok("修改失败");
    }

    @ApiOperation(value = "修改用户的基本信息(名字修改了)")
    @RequestMapping("adminUpdateNameUser")
    public Result adminUpdateNameUser(@RequestBody UserVo userVo) {
        //判断用户是否重名
        UserNameVo userNameVo  = new UserNameVo();
        String userName1 = userVo.getUserName();
        userNameVo.setUserName(userName1);
        List<UserNameVo> userNameVos = adminService.adminFoundUser(userNameVo);

        if (userNameVos.size()==0) {
            int result = adminService.adminUpdateUser(userVo);
            if (result > 0) {
                return Result.ok("修改成功");
            }
        }
        return Result.ok("修改失败,用户名重名了");
    }
}
